package com.skytech.component.crawler.web;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.scratch.IndexMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InitESIndexController {
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Value("${elasticsearch.server}")
    private String server;


    private static final Logger log = LoggerFactory.getLogger(InitESIndexController.class);
    @GetMapping("es/init")
    public JSONObject init(){
        try {
            restTemplateBuilder.build().delete(server+"mytest");
            restTemplateBuilder.build().put(server+"mytest",new JSONObject());
            restTemplateBuilder.build().put(server+"mytest/news/_mapping"+" -d", IndexMapping.CRAWLER_NEWS,JSONObject.class);
            /*restTemplateBuilder.build().put(server+"crawler_storage",new JSONObject());
            restTemplateBuilder.build().put(server+"crawler_news/storage/_mapping",IndexMapping.CRAWLER_STORAGE);*/
        }catch (Exception e){
            log.error("初始化索引失败{}",e.getMessage());
        }

        return new JSONObject();
    }



}
